<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选等级 -->
			<cl-filter label="等级">
				<cl-select prop="level" :options="options.level" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索用户名、昵称" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="member-info" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, reactive, ref } from "vue";

import { useDict } from "/$/dict";

const { dict } = useDict();

const { service } = useCool();

// 选项
const options = reactive({
	level: dict.get("memberLevel")
});

let anchorList = ref<any>([]);

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "用户名",
			prop: "username",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "密码",
			prop: "password",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "昵称",
			prop: "nickname",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "年龄",
			prop: "age",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "等级",
			prop: "level",
			component: { name: "el-select", options: options.level },
			value: 0,
			required: true
		},
		{
			label: "积分",
			prop: "points",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "余额",
			prop: "balance",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "订单",
			prop: "orderNumber",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "绑定主播",
			prop: "bindAnchor",
			component: {
				name: "el-select",
				options: anchorList,
				props: {
					multiple: true,
					filterable: true
				}
			},
			value: 0,
			required: false
		},
		{
			label: "状态",
			prop: "status",
			flex: false,
			component: { name: "cl-switch" },
			required: true
		},
		{
			label: "注册时间",
			prop: "registerTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" }
			},
			required: true
		},
		{
			label: "开通时间",
			prop: "openTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" }
			},
			required: true
		},
		{
			label: "最近登录",
			prop: "lastLogin",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" }
			},
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "用户名", prop: "username", minWidth: 140 },
		{ label: "密码", prop: "password", minWidth: 140 },
		{ label: "昵称", prop: "nickname", minWidth: 140 },
		{ label: "年龄", prop: "age", minWidth: 140 },
		{ label: "等级", prop: "level", dict: options.level, minWidth: 120 },
		{ label: "积分", prop: "points", minWidth: 140 },
		{ label: "余额", prop: "balance", minWidth: 140 },
		{ label: "订单", prop: "orderNumber", minWidth: 140 },
		{ label: "状态", prop: "status", minWidth: 100, component: { name: "cl-switch" } },
		{
			label: "注册时间",
			prop: "registerTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "开通时间",
			prop: "openTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "到期时间",
			prop: "expireTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.member.info
	},
	(app) => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}

const getAnchor = async () => {
	let a = await service.demo.info.list();
	anchorList.value = a.map((o) => {
		return {
			value: o.id,
			label: o.nickname
		};
	});
	console.log(anchorList.value, "aa");
};

onMounted(() => {
	getAnchor();
});
</script>
